package com.company.cloud.platform.modules.mgr.controller;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.api.request.RoleAddRequest;
import com.company.cloud.user.modules.api.request.RoleGet2DelRequest;
import com.company.cloud.user.modules.api.request.RoleQueryRequest;
import com.company.cloud.user.modules.api.request.RoleUpdateRequest;
import com.company.cloud.user.modules.api.response.RoleModel;
import com.company.cloud.user.modules.api.response.UserBaseResp;
import com.company.cloud.user.modules.api.service.RolePlatformRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Authc
@RestController
@RequestMapping("/admin/mgr/biz/role")
@Api(tags="Admin_MGR_APP角色管理")
public class RoleBizMgrController {


    @Autowired
    private RolePlatformRemoteService rolePlatformRemoteService;


    @PostMapping("/list")
    @ApiOperation(value="查询", response= RoleModel.class)
    public BaseResult list(@RequestBody RoleQueryRequest params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        if(params==null){
            params=new RoleQueryRequest();
        }
        BaseClientResult<List<RoleModel>> dataResult= rolePlatformRemoteService.roleList(params);
        List<RoleModel> data= BaseClientResult.getResultData2Error(dataResult);
        return BaseResult.ok(data);
    }


    @PostMapping("/add")
    @ApiOperation(value="新增", response=RoleModel.class)
    @StaffLogc(title = "新增APP角色", action = "新增APP角色", actionAdo = "APP角色管理")
    public BaseResult add(@RequestBody RoleAddRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<RoleModel> roleResult=rolePlatformRemoteService.addRole(params);
        RoleModel data= BaseClientResult.getResultData2Error(roleResult);
        return BaseResult.ok(data);
    }

    @PostMapping("/update")
    @ApiOperation(value="修改", response=RoleModel.class)
    @StaffLogc(title = "修改APP角色", action = "修改APP角色", actionAdo = "APP角色管理")
    public BaseResult update(@RequestBody RoleUpdateRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<RoleModel> roleResult = rolePlatformRemoteService.updateRole(params);
        RoleModel data= BaseClientResult.getResultData2Error(roleResult);
        return BaseResult.ok(data);
    }


    @GetMapping("/get")
    @ApiOperation(value="获取", response= RoleModel.class)
    public BaseResult get(@RequestParam("code")String code) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        RoleGet2DelRequest params=new RoleGet2DelRequest();
        params.setCode(code);
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<RoleModel> roleResult= rolePlatformRemoteService.infoRole(params);
        RoleModel data=BaseClientResult.getResultData2Error(roleResult);
        return BaseResult.ok(data);
    }


    @GetMapping("/del")
    @ApiOperation(value="删除", response= BaseResult.class)
    @StaffLogc(title = "删除APP角色", action = "删除APP角色", actionAdo = "APP角色管理")
    public BaseResult del(@RequestParam("code")String code) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        RoleGet2DelRequest params=new RoleGet2DelRequest();
        params.setCode(code);
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<String> baseResp = rolePlatformRemoteService.delRole(params);
        BaseClientResult.getResultData2Error(baseResp);
        return BaseResult.ok();
    }

}
